#include "CGame.h"
#include "Header.h"
#include "CStateManagement.h"
#include "CDevice.h"
#include "CFpsController.h"
#include "CViewController.h"

namespace GameTutor
{
	CGame::CGame(): m_isAlive(true), m_isPause(false) 
	{
		s_pInstance = this;
	}

	void CGame::Pause()
	{
		m_isPause = true;
	}

	void CGame::Resume()
	{
		m_isPause = false;
	}

	void CGame::Exit()
	{
		m_isAlive = false;
	}

	void CGame::Run()
	{
		this->Init();

		// Init View
		if (!CViewController<VIEWCLASS>::GetInstance())
		{
			CViewController<VIEWCLASS>::GetInstance()->CreateView(640, 480);
		}
		while(m_isAlive)
		{
			CFpsController::GetInstance()->BeginCounter();
			if (m_isPause)
			{
				CStateManagement::GetInstance()->Update(true);
			} 
			else
			{
				CStateManagement::GetInstance()->Update(false);
			}
			CViewController<VIEWCLASS>::GetInstance()->GetView()->Update();
			CFpsController::GetInstance()->EndCounter();			
		}

		CStateManagement::GetInstance()->SwitchState(0);
		CStateManagement::GetInstance()->Update(false);

		delete CViewController<VIEWCLASS>::GetInstance();

		Destroy();
	}
}